package org.example;

import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ExcelUtility {

    public static void removeBlankRows(String inputFile, String outputFile) {
        System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.ctc.wstx.stax.WstxInputFactory");

        System.out.println("-->开始删除空白行");

        try {
            FileInputStream inputStream = new FileInputStream(inputFile);
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            // 创建新的工作表
            System.out.println("-->创建新的工作表");
            Workbook newWorkbook = WorkbookFactory.create(true);
            Sheet newSheet = newWorkbook.createSheet(sheet.getSheetName());

            System.out.println("-->获取最后一行的索引");
            // 获取最后一行的索引
            int lastRowNum = sheet.getLastRowNum();

            System.out.println("-->复制非空行到新的工作表");
            // 复制非空行到新的工作表
            int newRowNum = 0;
            for (int rowIndex = 0; rowIndex <= lastRowNum; rowIndex++) {
                Row row = sheet.getRow(rowIndex);
                if (row != null && !isRowEmpty(row)) {
                    Row newRow = newSheet.createRow(newRowNum);
                    copyRow(row, newRow);
                    newRowNum++;
                }
            }

            inputStream.close();

            System.out.println("-->写入新的Excel文件");
            // 写入新的Excel文件
            FileOutputStream outputStream = new FileOutputStream(outputFile);
            newWorkbook.write(outputStream);
            outputStream.close();

            System.out.println("-->空白行已成功删除并保存");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 判断行是否为空
    private static boolean isRowEmpty(Row row) {
        if (row == null) {
            System.out.println("-->判断行是否为空");
            return true;
        }

        for (Cell cell : row) {
            System.out.println("-->判断行是否为空");
            if (cell.getCellType() != CellType.BLANK) {
                System.out.println("-->false");
                return false;
            }
        }
        System.out.println("-->true");
        return true;
    }

    // 复制行数据
    private static void copyRow(Row sourceRow, Row newRow) {
        newRow.setHeight(sourceRow.getHeight());

        for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
            System.out.println("-->复制行数据");
            Cell sourceCell = sourceRow.getCell(i);
            if (sourceCell != null) {
                Cell newCell = newRow.createCell(i, sourceCell.getCellType());

                switch (sourceCell.getCellType()) {
                    case BOOLEAN -> newCell.setCellValue(sourceCell.getBooleanCellValue());
                    case ERROR -> newCell.setCellValue(sourceCell.getErrorCellValue());
                    case FORMULA -> newCell.setCellFormula(sourceCell.getCellFormula());
                    case NUMERIC -> newCell.setCellValue(sourceCell.getNumericCellValue());
                    case STRING -> newCell.setCellValue(sourceCell.getRichStringCellValue());
                    default -> {
                    }
                }
            }
        }
    }
}
